<?php
 /*****************************************************************************
 * download.php                                                               *
 * Last Modified: 2007-06-07                                                  *
 *                                                                            *
 * download.php allows the user to download a specified file by getting the   *
 * the file's ID from viewContact.php.                                        *
 *                                                                            *
 * BBG_Billing, a PHP application using MySQL for creating and maintaining a  *
 * contacts and invoices database.                                            *
 *                                                                            *
 * @copyright Copyright (C) 2007, Bugs Bee Gone Computer Services             *
 * @owner     Daniel Barnett (Bugs Bee Gone Computer Services)                *
 * @author    Daniel Barnett <dbarnett@bugsbeegone.com>                       *
 * @website   http://www.bugsbeegone.com                                      *
 * @license   http://www.gnu.org/licenses/gpl.html GNU General Public License *
 * @package   BBG_Billing                                                     *
 * @name      download.php                                                    *
 * @version   1.0.0                                                           *
 *                                                                            *
 * This file is part of the "BBG_Billing" PHP application.                    *
 *                                                                            *
 * BBG_Billing is free software; you can redistribute it and/or modify        *
 * it under the terms of the GNU General Public License as published by       *
 * the Free Software Foundation; either version 2 of the License, or          *
 * (at your option) any later version.                                        *
 *                                                                            *
 * BBG_Billing is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU General Public License for more details.                               *
 *                                                                            *
 * You should have received a copy of the GNU General Public License          *
 * along with BBG_Billing; if not, write to the Free Software                 *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
 *****************************************************************************/


    // Require session.php for user access control
    require "include/session.php";

    // If user is not logged in, redirect to main.php
    if(!$session->logged_in){
        header("Location: main.php");
        exit();
    }

    // if id is set then get the file with the id from database
    if(isset($_GET['id'])) {
        // Require config.inc.php for configuration variables
        require_once "config.inc.php";

        // Connect to MySQL
        mysql_connect($path, $username, $password);
        // Select the MySQL database, if error die with error message
        @mysql_select_db($database) or die("Unable to select database.<br />" . mysql_error());

        // Get id value from URL
        $id = $_GET['id'];

        // Define MySQL query to retrieve the specified file
        $query = "SELECT `name`, `type`, `size`, `content` FROM tblSysInfo WHERE `key` = $id";

        // Execute the MySQL query, if error die with error message
        $result = mysql_query($query) or die('Error, query failed<br />' . mysql_error());

        // Retrieve the file parameters
        list($name, $type, $size, $content) = mysql_fetch_array($result);

        // Define the document headers from the retrieved file
        header("Content-length: $size");
        header("Content-type: $type");
        header("Content-Disposition: attachment; filename=$name");

        // Output the file
        echo $content;

        // Close the connection
        mysql_close();

        // Exit this script
        exit;
    }
?>
